package com.newtouch.bxzs.common.base.util;

import com.newtouch.bxzs.common.base.model.bo.UserDetailBO;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;

import java.util.Map;

/**
 * 获取登录用户信息工具类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年1月13日</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author Amour
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
public class UserDetailUtil {

	public static UserDetailBO getUserDetail() {
		final Jwt jwt = (Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		final Map<String, Object> claims = jwt.getClaims();
		try {
			UserDetailBO.UserDetailBOBuilder bulider = UserDetailBO.builder()
					.userId(String.valueOf(claims.get("userId")))
					.userJobNumber(String.valueOf(claims.get("userJobNumber")))
					.userName(String.valueOf(claims.get("userName")))
					.userLoginAccountNumber(String.valueOf(claims.get("userLoginAccountNumber")))
					.userCertificateType(String.valueOf(claims.get("userCertificateType")))
					.userCertificateNumber(String.valueOf(claims.get("userCertificateNumber")))
					.userOrganizationCode(String.valueOf(claims.get("userOrganizationCode")))
					.userOrganizationName(String.valueOf(claims.get("userOrganizationName")))
					.userPhone(String.valueOf(claims.get("userPhone")));
			return bulider.build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
